import ColumnAddDel, { onAddAndDelType } from "./components/ColumnAddDel";
import { IColumnType } from "./interface";

export declare type IColumnsType<RecordType = unknown> =
    IColumnType<RecordType>[];
interface IColumnsOptions {
    isAdd?: boolean;
    isDel?: boolean;
    dataSourceRef?: any;
    onAddAndDel?: onAddAndDelType;
}
const useParseColumns = (_columns: IColumnsType | undefined) => {
    return _columns?.filter?.((item: any) => item.isDisplay !== false);
};
export const useColumns = (
    _columns: IColumnsType | undefined,
    options: IColumnsOptions
) => {
    const { isAdd, isDel, onAddAndDel } = options;
    const columns = useParseColumns(_columns);

    const deepColumns: IColumnsType = columns ? [...columns] : [];
    let newColumns: IColumnsType = deepColumns;

    if (isAdd || isDel) {
        const newColumn: any = {
            title: "",
            width: 42,
            fixed: "left",
            align: "center",
            render: (text: any, record: any, index: number) => {
                return (
                    <ColumnAddDel
                        isAdd={isAdd}
                        isDel={isDel}
                        index={index}
                        onAddAndDel={onAddAndDel}
                    />
                );
            },
        };
        newColumns.unshift(newColumn);
    }
    return { newColumns };
};
